<template>
  <div class="storedDetails">
    <el-card>
      <el-button type="primary" @click="changePassWord" style="margin-bottom:20px">更改储值密码</el-button>
      <avue-crud
        ref="crud"
        style="width:100%"
        :data="loadData"
        :option="tableOption"
        :table-loading="loading"
      ></avue-crud>
      <template>
        <div style="width:100%;position:relative;height:100px">
          <el-pagination
            :page-sizes="[10, 20,30,40, 50, 100]"
            :pager-count="7"
            :current-page="page.current"
            :page-size="page.size"
            :total="page.total"
            layout="total, sizes, prev, pager, next, jumper"
            style="margin:2% 0;position:absolute;right:1%"
            background
            @size-change="sizeChange"
            @current-change="currentChange"
          ></el-pagination>
        </div>
      </template>
    </el-card>
    <el-dialog title="更改储值密码" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="120px"
        class="demo-ruleForm"
      >
        请验证会员身份信息，验证成功后会将新的密码短信发送至会员手机
        <el-row :gutter="10" class="row">
          <el-col :xs="24" :sm="24">
            <el-form-item label="手机号：" prop="phoneNum">
              <el-input v-model="ruleForm.phoneNum" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24">
            <el-form-item label="验证码：" prop="code">
              <el-col :xs="24" :sm="24" style="display:flex">
                <el-input v-model="ruleForm.code" style="margin-right:5px"></el-input>
                <el-button type="primary" @click="sendCode" v-show="show">发送验证码</el-button>
                <el-button type="primary" v-show="!show">{{count}}</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handlClose">取 消</el-button>
        <el-button type="primary" @click="handlSure" :loading="loadingSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  storedDetailedList,
  updateStoredbyPhone,
  sendMessage,
} from "@/api/shop/member/list.js";
import { isMobile } from "@/util/validate.js";

export default {
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        if (isMobile(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    var checkYzm = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("验证码不能为空"));
      } else {
        if (value.length != 6) {
          return callback(new Error("请输入6位有效验证码"));
        } else {
          callback();
        }
      }
    };
    return {
      count: "",
      timer: null,
      show: true,
      dialogVisible: false,
      ruleForm: {},
      loading: false,
      loadingSure: false,
      page: {
        total: 0,
        size: 10,
        current: 1,
      },
      rules: {
        phoneNum: [{ required: true, validator: checkPhone, trigger: "blur" }],
        code: [{ required: true, validator: checkYzm, trigger: "blur" }],
      },
      loadData: [
        {
          bhlx: "1212",
        },
      ],
      tableOption: {
        header: false,
        align: "center",
        editBtn: false,
        delBtn: false,
        menu: false,
        column: [
          {
            label: "储值变化类型",
            prop: "operationType",
            minWidth: 120,
            overHidden: true,
            dicData: [
              {
                label: "充值",
                value: "01",
              },
              {
                label: "消费",
                value: "02",
              },
            ],
          },
          {
            label: "储值变化（元）",
            prop: "operationNum",
            minWidth: 120,
            overHidden: true,
          },
          {
            label: "储值规则",
            prop: "storedRuleName",
            minWidth: 120,
            overHidden: true,
          },
          {
            label: "储值余额（元）",
            prop: "storedBalance",
            minWidth: 120,
            overHidden: true,
          },
          {
            label: "支付方式",
            prop: "payType",
            dicData: [
              {
                label: "支付宝",
                value: "01",
              },
              {
                label: "微信",
                value: "02",
              },
            ],
            minWidth: 120,
            overHidden: true,
          },
          {
            label: "支付金额（元）",
            prop: "payAmt",
            minWidth: 120,
            overHidden: true,
          },
          {
            label: "渠道",
            prop: "storedSource",
            minWidth: 120,
            overHidden: true,
            dicData: [
              {
                label: "线上",
                value: "00",
              },
              {
                label: "线下",
                value: "01",
              },
            ],
          },
          {
            label: "门店",
            prop: "shopName",
            width: 120,
            overHidden: true,
          },
          {
            label: "操作人",
            prop: "operatorId",
            width: 120,
            overHidden: true,
          },
          {
            label: "变动时间",
            prop: "createTime",
            width: 120,
            overHidden: true,
          },
        ],
      },
    };
  },
  methods: {
    getList() {
      storedDetailedList(
        Object.assign({ personId: this.$route.query.personId }, this.page)
      ).then((val) => {
        let data = val.data.data;
        this.page = {
          total: data.total,
          size: data.size,
          current: data.current,
        };
        this.loadData = data.records;
        console.log(this.loadData);
      });
    },
    handlClose() {
      this.dialogVisible = false;
      this.loadingSure = false
      clearInterval(this.timer);
      this.timer = null;
      this.show = true
      this.$refs["ruleForm"].resetFields();
    },
    handlSure() {
      this.ruleForm.personId = this.$route.query.personId;
      this.ruleForm.saltValue = this.$route.query.saltValue;
      this.loadingSure = true
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          updateStoredbyPhone(this.ruleForm).then((val) => {
            this.getList();
            this.handlClose();
            this.$message({
              message:
                "储值密码修改成功，密码以短信形式发送至手机，请注意查收！",
              type: "success",
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      }).catch(()=>{
        this.loadingSure = false
      });
    },
    changePassWord() {
      this.dialogVisible = true;
      this.ruleForm = {};
      this.$set(
        this.ruleForm,
        "phoneNum",
        this.$store.getters.memberInfo.phoneNum
          ? this.$store.getters.memberInfo.phoneNum
          : ""
      );
    },
    sizeChange(size) {
      this.page.size = size;
      this.getList();
    },
    currentChange(current) {
      this.page.current = current;
      this.getList();
    },
    sendCode() {
      sendMessage({ phone: this.ruleForm.phoneNum }).then((val) => {
        const TIME_COUNT = 60;
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.storedDetails {
  .row {
    margin: 30px 0px;
  }
  .bg {
    width: 202px;
    height: 33px;
    text-align: center;
    line-height: 33px;
    background-color: rgba(242, 242, 242, 1);
    font-weight: 400;
    font-style: normal;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.647058823529412);
  }
}
</style>